package com.vrv.controller.analysis;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.vrv.entity.analysis.ViewInfo;
import com.vrv.utils.Constans;
import com.vrv.utils.HttpClientUtil;
import com.vrv.utils.JsonUtil;
import com.vrv.utils.MyGson;
import com.vrv.utils.StringUtil;
import com.vrv.utils.XMLRead;

@Controller
@RequestMapping("/analysis/view")
public class ViewsController {

    @Autowired
    public HttpServletRequest request;

    @ModelAttribute("fullURL")
    public String baseURL(HttpServletRequest request) {
        return request.getScheme() + "://" + request.getServerName() + ":"
                + request.getServerPort() + request.getContextPath() + "/";
    }

    @RequestMapping(value = "/viewlist")
    public String viewlist(ModelMap model) {
        String target = XMLRead.HND_IP + "viewlist";
        String resultData = HttpClientUtil.getInstance().get(target);
        List<ViewInfo> list = MyGson.toList(resultData);
        model.put("list", list);
        return "/analysis/view_list";
    }

    @RequestMapping(value = "/toAdd")
    public String toAdd(ModelMap model) {
        return "/analysis/view_edit";
    }

    @RequestMapping(value = "/viewportal", produces = Constans.PRODUCES)
    public String viewportal(ModelMap model, String id, String viewrows, String viewcols) {
        String target = XMLRead.HND_IP + "viewportal?id=" + id;
        String resultData = HttpClientUtil.getInstance().get(target);
        List<Map<String, Object>> list = MyGson.toList(resultData);
        Map<String, Object> map = new HashMap<String, Object>();
        if (!StringUtil.listEmpty(list)) {
            for (Map<String, Object> map2 : list) {
                map.put(map2.get("viewarea") + "", map2);
            }
        }
        model.put("viewlist", map);
        model.put("viewrows", viewrows);
        model.put("viewcols", viewcols);
        System.out.println(JsonUtil.MapToJson(map));
        model.put("viewjson", JsonUtil.MapToJson(map));
        return "/analysis/view_portal";
    }

    @RequestMapping(value = "/getViewURL", produces = Constans.PRODUCES)
    @ResponseBody
    public String getViewURL(String id) {
        String target = XMLRead.HND_IP + "getViewURL?id=" + id;
        String jsonString = HttpClientUtil.getInstance().get(target);
        return jsonString;
    }

    @RequestMapping(value = "/delview", produces = Constans.PRODUCES)
    @ResponseBody
    public String delview(String id) {
        String target = XMLRead.HND_IP + "delview?id=" + id;
        String jsonString = HttpClientUtil.getInstance().get(target);
        return jsonString;
    }

    @RequestMapping(value = "/addView", produces = Constans.PRODUCES)
    @ResponseBody
    public String addView(String viewname, String rows, String column) {
        String content = "";
        content += "?viewname=" + viewname + "&row=" + rows + "&column=" + column;
        String target = XMLRead.HND_IP + "addView";
        String jsonString = HttpClientUtil.getInstance().get(target + content);
        return jsonString;
    }

    @RequestMapping(value = "/changeStatus", produces = Constans.PRODUCES)
    @ResponseBody
    public String changeViewEnable(String id, Integer status) {
        String target = "";
        if (null != status) {
            if (0 == status) {
                target = XMLRead.HND_IP + "changeViewEnable?status=0&id=" + id;
            } else {
                target = XMLRead.HND_IP + "changeViewDisable?status=1&id=" + id;
            }
        }
        String jsonString = HttpClientUtil.getInstance().get(target);

        return jsonString;
    }

}
